// pages/knowledge/index.js
const app = getApp()
var api = require('../../config/api.js');
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    navHeight:app.globalData.appInfo.navHeight,
    /** 页面各项数量数据 */
    knowledgePageData: {},
    /** 页面初始化数据 */
    currentPage: 1, //请求业索引
    pageSize: 5, //请求业数据大小
    pages: 0, //总页数
    totalSize: 0, //条目总数
    caseInfoList: [], //案件列表
    pickerSelected: false,
    filterType: 'all',
    active: 'dealing',
    activeType: 'waitWrite', //数据类别
    activeTypeValue: '待撰写',
    selectList: [{
        label: '外观设计',
        value: 'design'
      },
      {
        label: '著作权',
        value: 'copyright'
      },
      {
        label: '商标',
        value: 'trademark'
      }
    ]
  },
  changeType(e) {
    const {
      type
    } = e.currentTarget.dataset;
    this.setData({
      filterType: type,
      pickerSelected: true,
      currentPage: 1
    });
    if (type === 'all') {
      this.setData({
        pickerSelected: false
      });
    }
    this.getKnowledgeCaseList(this.data.activeType);
  },

  //处理中、已申报、已结案、年费监控  选择事件
  selectCard(e) {
    const {
      active
    } = e.currentTarget.dataset;
    this.setData({
      active: active,
      currentPage: 1
    });
    this.getKnowledgeCaseList(this.data.activeType);
  },

  //案件类别选择事件（例如待撰写、已撰写、受审、实审...）
  selectFilter(e) {
    var activeType = e.currentTarget.dataset.active;
    var activeTypeValue = e.currentTarget.dataset.activevalue;
    this.setData({
      activeType: activeType,
      activeTypeValue: activeTypeValue,
      currentPage: 1
    });
  },

  //案件详情跳转
  editItem(e) {
    wx.navigateTo({
      url: '../case-info/index?caseNo=' + e.currentTarget.dataset.caseno,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //调用加载数据的方法
    this.loadPageData();
    //获取案件列表数据
    this.getKnowledgeCaseList(this.data.activeType);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

    var that = this;
    var currentPage = that.data.currentPage;
    console.log(currentPage);
    that.setData({
      currentPage: currentPage + 1
    });
    that.getKnowledgeCaseList(that.data.activeType);
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  /** 页面加载获取数据 */
  loadPageData: function () {
    var that = this;
    util.request(api.getJqKnowledgeData, {
      userId: wx.getStorageSync('appUserInfo').id,
      type: 'waitWrite'
    }, 'GET').then(res => {
      if (res.result == 1) {
        that.setData({
          knowledgePageData: res.info.knowledgePageData
        });
      } else {

      }
    });
  },

  //获取案件列表list
  getKnowledgeCaseList: function () {
    var that = this;
    util.request(api.getKnowledgeCaseList, {
      currentPage: that.data.currentPage,
      pageSize: that.data.pageSize,
      userId: wx.getStorageSync('appUserInfo').id,
      activeType: that.data.activeType,
      active: that.data.active,
      filterType: that.data.filterType
    }, 'GET').then(res => {
      if (res.result == 1) {
        var list = res.info.list;
        var tempList = that.data.caseInfoList;
        var tempCurrentPage = that.data.currentPage;
        if (that.data.currentPage == 1) {
          tempList = list;
          tempCurrentPage = 1;
          wx.stopPullDownRefresh();
        } else {
          tempList = tempList.concat(list);
        }
        that.setData({
          currentPage: tempCurrentPage,
          pageSize: res.info.pageSize,
          pages: res.info.pages,
          totalSize: res.info.totalSize,
          caseInfoList: tempList
        });
      }
    });
  },
})